<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Log;
use App\Repositories\Eloquent\MemberRepository;

use Auth, DB, Hash, Redirect;

class WeixinController extends Controller
{
    protected $member;
    
    /**
     * 构造方法，创建MemberRepository实例
     * @param MemberRepository $member [description]
     */
    public function __construct(MemberRepository $member)
    {
        parent::__construct();

        $this->member = $member;
    }


    /**
     * 登录成功后首页
     * @return [type] [description]
     */
    public function index() 
    {
        $member = Auth::guard()->user();

        return view($this->view, compact('member'));
    }


    /**
     * 生成个人二维码
     * @return [type] [description]
     */
    public function qrcode()
    {
        $member = Auth::guard()->user();

        $member->mobile = substr_replace($member->mobile, '****', 3, 4);

        $qrcode = $this->member->qrcode($member);

        return view($this->view, compact('member'));
    }


    /**
     * 个人名下应聘信息
     * @return [type] [description]
     */
    public function list()
    {
        $member = Auth::guard()->user();
        
        $lists = $member->children()
                 ->orderBy('date','desc')
                 ->get();

        $groups = $member->children()
                ->with('children')
                ->has('children','>',0)
                ->orderBy('date','desc')
                ->get();

        $groups->map(function ($item, $key) {
            $item->number = $item->number();
        });

        return view($this->view, compact('member', 'lists','groups'));
    }


    public function group()
    {
        $member = Auth::guard()->user();

        $group = $member->children()->get();

        $group->map(function ($item, $key) {
            $item->number = $item->number();
        });
        
        return view($this->view, compact('member', 'group'));
    }


    public function others($uuid)
    {
        $member = $this->member->show($uuid);

        $others = $member->children()
                ->orderBy('date','desc')
                ->get();

        return view($this->view, compact('member', 'others'));
    }


    public function show($uuid)
    {
        $member = Auth::guard()->user();

        $recruit = $this->member->mine($member, $uuid);

        if ($recruit instanceof JsonResponse && $recruit->getStatusCode() === 404) {
            return abort('404');
        }

        return view($this->view, compact('member', 'recruit'));
    }


    public function team($uuid)
    {
        $member = Auth::guard()->user();

        $teams = $this->member->teams($member, $uuid);

        if ($teams instanceof JsonResponse && $teams->getStatusCode() === 404) {
            return abort('404');
        }
        
        $teams->map(function ($item, $key) {
            $item->number = $item->number();
        });

        

        return view($this->view, compact('member', 'teams'));
    }


    /**
     * 应聘审核
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function verify(Request $request, $uuid)
    {
        $member = Auth::guard()->user();

        return $this->member->verify($member, $uuid, $request);
    }


    public function change()
    {
        $member = Auth::guard()->user();

        return view($this->view, compact('member'));
    }


    public function postChange(Request $request)
    {
        $member = Auth::guard()->user();

        return $this->member->renew($member, $request);
    }

    /**
     * 退出
     * @return [type] [description]
     */
    public function logout()
    {        
        Auth::guard()->logout();

        return Redirect::to('/');
    }


    /**********************************/

    public function rank()
    {
        $member = Auth::guard()->user();

        $ranks = $this->member->ranks();

        return view($this->view, compact('member','ranks'));
    }


    public function manage()
    {
        $member = Auth::guard()->user();

        return view($this->view, compact('member'));
    }
}